package org.example.trigger.http;


import lombok.extern.slf4j.Slf4j;
import org.example.api.IShopApi;
import org.example.api.dto.ShopQueryResDTO;
import org.example.api.response.Response;
import org.example.domain.shop.model.entity.ProductEntity;
import org.example.domain.shop.service.IShopService;
import org.example.types.enums.ResponseCode;
import org.example.types.exception.AppException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yinghuashen
 * @version 1.0
 * @description
 * @date 2025/3/7 10:40
 */

@Slf4j
@RestController
@CrossOrigin("*")
@RequestMapping("/shop")
public class ShopController implements IShopApi {

    @Resource
    private IShopService shopService;

    @Override
    @PostMapping("/queryById")
    public Response<ShopQueryResDTO> getById(@RequestParam String id) {
        try {
            ProductEntity product = shopService.getProductById(id);
            ShopQueryResDTO shopQueryResDTO = ShopQueryResDTO.builder()
                    .productId(product.getProductId())
                    .productName(product.getProductName())
                    .productDescription(product.getProductDescription())
                    .availableCount(product.getAvailableCount())
                    .soldCount(product.getSoldCount())
                    .originalPrice(product.getOriginalPrice())
                    .discountPrice(product.getDiscountPrice())
                    .build();
            return Response.<ShopQueryResDTO>builder().code(ResponseCode.SUCCESS.getCode()).info(ResponseCode.SUCCESS.getInfo()).data(shopQueryResDTO).build();
        } catch (AppException e) {
            return Response.<ShopQueryResDTO>builder().code(e.getCode()).info(e.getInfo()).data(null).build();
        }
    }

    @Override
    @PostMapping("/queryByKeyword")
    public Response<List<ShopQueryResDTO>> getByKeyword(@RequestParam String keyword, @RequestParam int pageNum, @RequestParam int pageSize) {
        try {
            // 调用服务层的分页查询方法
            List<ProductEntity> productList = shopService.getProductListByKeyword(keyword, pageNum, pageSize);
            if(productList == null){
                return Response.<List<ShopQueryResDTO>>builder()
                        .code(ResponseCode.SUCCESS.getCode())
                        .info(ResponseCode.SUCCESS.getInfo())
                        .data(null)
                        .build();
            }

            // 将 ProductEntity 列表转换为 ShopQueryResDTO 列表
            List<ShopQueryResDTO> shopQueryResDTOList = productList.stream()
                    .map(product -> ShopQueryResDTO.builder()
                            .productId(product.getProductId())
                            .productName(product.getProductName())
                            .productDescription(product.getProductDescription())
                            .availableCount(product.getAvailableCount())
                            .soldCount(product.getSoldCount())
                            .originalPrice(product.getOriginalPrice())
                            .discountPrice(product.getDiscountPrice())
                            .build())
                    .collect(Collectors.toList());
            // 返回成功响应
            return Response.<List<ShopQueryResDTO>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(shopQueryResDTOList)
                    .build();
        } catch (AppException e) {
            // 处理异常情况
            return Response.<List<ShopQueryResDTO>>builder()
                    .code(e.getCode())
                    .info(e.getInfo())
                    .data(null)
                    .build();
        }
    }
}
